home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / dev / lang / sofa.lha / sofa / smalleiffel / sys / runtime / trace.c < prev    next >
C/C++ Source or Header  |  2000-03-30  |  5KB  |  190 lines

  1. /*
  2. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  3. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  4. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  5. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  6. -- this header is kept unaltered, and a notification of the changes is added.
  7. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  8. -- another product.
  9. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  10. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  11. --                       http://SmallEiffel.loria.fr
  12. --
  13. */
  14.  
  15. /*
  16.   This file (SmallEiffel/sys/runtime/trace.c) is automatically included when 
  17.   `run_control.no_check' is true (ie. all modes except -boost).
  18.   This file comes after no_check.[hc] to implements the -trace flag.
  19. */
  20.  
  21. #ifdef SE_TRACE
  22. /*
  23.   The SmallEiffel -trace is used, so the command line SmallEiffel 
  24.   step-by-step se_trace fucntion is defined.
  25. */
  26. static FILE* se_trace_file = NULL;
  27. static int se_write_trace_flag = 0;
  28. static int se_trace_ready_flag = 0;
  29. static int se_step_by_step_flag = 1;
  30.  
  31. int get_answer(void) {
  32.   int result = 0;
  33.   char c = getc(stdin);
  34.   if (c != '\n') {
  35.     result = c;
  36.   }
  37.   while (c != '\n') {
  38.     c = getc(stdin);
  39.   }
  40.   return result;
  41. }
  42.  
  43. static void sedb_help_command(void) {
  44.   printf("SmallEiffel debugger.\n");
  45.   printf("List of classes of commands:\n");
  46.   printf("   (h) Help.\n");
  47.   printf("   (s) Stack view.\n");
  48.   printf("   (q) Quit.\n");
  49.   printf("   Return to see the current Eiffel source line.");
  50.   printf("   \n");
  51.   printf("Please, feel free to debug or to complete this simple\n");
  52.   printf("step-by-step debugger (see source file in\n");
  53.   printf("\"SmallEiffel/sys/runtime/trace.c\".\n");
  54. }
  55.  
  56. static void sedb_show_source_line(int l,int c,int f) {
  57.   static int f_memo = 0;
  58.   if (p[f] == NULL) {
  59.     printf("line %d column %d of ???\t\t",l,c);
  60.   }
  61.   else {
  62.     FILE *file = fopen(p[f],"r");
  63.     if (file != NULL) {
  64.       int line = 1;
  65.       int column = 1;
  66.       char cc;
  67.       while (line < l) {
  68.     cc = fgetc(file);
  69.     if (cc == '\n') {
  70.       line++;
  71.     }
  72.       }
  73.       cc = fgetc(file);
  74.       while (cc != '\n') {
  75.     if (cc == '\t') {
  76.       printf("        ");
  77.       column+=7;
  78.     }
  79.     else {
  80.       fputc(cc,stdout);
  81.     }
  82.     cc = fgetc(file);
  83.     column++;
  84.       }
  85.       while (column < 72) {
  86.     fputc(' ',stdout);
  87.     column++;
  88.       }
  89.       printf("l%dc%d ",l,c);
  90.       if (f_memo != f) {
  91.     printf(" %s ",p[f]);
  92.       }
  93.       f_memo = f;
  94.       fclose(file);
  95.     }
  96.     else {
  97.       printf("line %d column %d of %s\t\t",l,c,p[f]);
  98.     }
  99.   }
  100. }
  101.  
  102. void se_trace(se_dump_stack*ds, se_position position) {
  103.   static char cmd_memo = 1;
  104.   static char cmd;
  105.   int l = se_position2line(position);
  106.   int c = se_position2column(position);
  107.   int f = se_position2path_id(position);
  108.   ds->p = position;
  109.   if (se_trace_ready_flag) {
  110.     if (se_write_trace_flag) {
  111.       fprintf(se_trace_file,"line %d column %d in %s\n",l,c,p[f]);
  112.       fflush(se_trace_file);
  113.     }
  114.   next_command:
  115.     if (se_step_by_step_flag) {
  116.       if (cmd_memo != 0) {
  117.     printf("(sedb) ");
  118.       }
  119.       fflush(stdout);
  120.       cmd = get_answer();
  121.       cmd_memo = cmd;
  122.       if ((cmd == 'h') || (cmd == 'H') || (cmd == '?')) {
  123.     sedb_help_command();
  124.     goto next_command;
  125.       }
  126.       else if ((cmd == 's') || (cmd == 'S')) {
  127.     se_print_run_time_stack();
  128.     goto next_command;
  129.       }
  130.       else if ((cmd == 'q') || (cmd == 'Q')) {
  131.     exit(EXIT_FAILURE);
  132.       }
  133.       else if (cmd == 0) {
  134.     sedb_show_source_line(l,c,f);
  135.       }
  136.       else {
  137.     printf("Unknown command.\nTtype H for help\n");
  138.     goto next_command;
  139.       }
  140.     }
  141.   }
  142.   else {
  143.     se_trace_ready_flag = 1;
  144.     printf("Write the execution trace in \"trace.se\" file (y/n) ? [n]");
  145.     fflush(stdout);
  146.     if (get_answer() == 'y') {
  147.       se_write_trace_flag = 1;
  148.       se_trace_file = fopen("trace.se","w");
  149.     }
  150.     printf("Step-by-step execution (y/n) ? [y]");
  151.     fflush(stdout);
  152.     if (get_answer() == 'n') {
  153.       se_step_by_step_flag = 0;
  154.     }
  155.     else {
  156.     sedb_help_command();
  157.     }
  158.   }
  159. }
  160. #endif
  161.  
  162.  
  163. #ifdef SE_WEDIT
  164. /* 
  165.    Smooth interface with Wedit debugger.
  166. */
  167. #define MAXBREAKPOINTS 256
  168. static int __BreakpointsList[MAXBREAKPOINTS];
  169. void SE_CallDebugger(void) {
  170. }
  171.  
  172. void se_trace(se_dump_stack*ds,se_position p) {
  173.   int l = se_position2line(p);
  174.   int c = se_position2column(p);
  175.   int f = se_position2path_id(p);
  176.   int i,s;
  177.   
  178.   ds->p = p;
  179.   s = (f <<16)|l;
  180.   for (i=0; i< MAXBREAKPOINTS;i++) {
  181.     if (__BreakpointsList[i] == s) {
  182.       SE_CallDebugger();
  183.     }
  184.     else if (__BreakpointsList[i] == 0)
  185.       break;
  186.   }
  187. }
  188. #endif
  189.  
  190.